var Pager = function() {
    this.init = function (cfg) {
        this.currentPage = 1;
        this.total = cfg.total;
        this.limit = cfg.limit;
        this.pages = getPages();
        this.showNum = cfg.showNum;
    };
    var that = this;
    var getPages = function () {
        return Math.ceil(that.total/that.limit);
    };
    this.click = function(page) {
        that.currentPage = page;
        pageshow();
        return that.currentPage;
    };
    this.next = function() {
        that.currentPage ++;
        pageshow();
        return that.currentPage;
    };

    this.prev = function() {
        that.currentPage --;
        pageshow();
        return that.currentPage;
    };

    var pageshow = function() {
        var num = Math.ceil(that.currentPage/that.showNum);
        var start = (num - 1) * that.showNum;
        var end = num * that.showNum;

        $('a.g_page').removeClass('g_active');
        $('a.g_page').removeClass('none');
        for(var i = 1; i <= that.showNum; i ++) {
            var page = (num - 1) * that.showNum + i;
	    if(page > that.pages) {
                $('a.g_page').eq(i).addClass('none');
            }
            if(page == that.currentPage) {
                $('a.g_page').eq(i).addClass('g_active');
            }
            $('a.g_page').eq(i).text(page);
            if(that.currentPage == 1) {
                $('a.g_prev').addClass('g_active');
            }
            if(that.currentPage == that.pages) {
                $('a.g_next').addClass('g_active');
            }
        }
        return {start:start,end:end};
    };

    this.showPages = function () {
        var i = 1;
        var tmp = '';
        if(that.pages <= 1) return false;
	if(that.currentPage == 1) {
            tmp = 'g_active';
        }
        var content = "<span>共 <strong>"+that.pages+"</strong>页</span>";
        content += "<a class='g_page g_prev "+tmp+"'>上一页</a>";
        while (i <= that.showNum) {
            content += "<a class='g_page'></a>";
            i ++;
        }
        tmp = '';
        if(that.currentPage == that.pages) {
            tmp = 'g_active';
        }
        content += "<a class='g_page g_next "+tmp+"'>下一页</a>";
	$("#page").html(content);

        pageshow();
    }

};
